package com.FengChaoSystem.service.CouponService;

import com.FengChaoSystem.dao.CouponDao;
import com.FengChaoSystem.pojo.Coupon;
import com.FengChaoSystem.service.CouponImpl;
import com.FengChaoSystem.untils.SqlSessionFactoryUtils;
import com.Mybatis.session.SqlSession;
import com.Mybatis.session.SqlSessionFactory;

import java.util.List;

public class CouponService implements CouponImpl {
    SqlSessionFactory sqlSessionFactory= SqlSessionFactoryUtils.getSqlSessionFactory();
    @Override
    public List<Coupon> hasCoupon(Coupon target) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CouponDao mapper = sqlSession.getMapper(CouponDao.class);
        List<Coupon> result = mapper.hasCoupon(target);
        sqlSession.close();
        return result;
    }

    @Override
    public void addCoupon(Coupon coupon) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CouponDao mapper = sqlSession.getMapper(CouponDao.class);
        mapper.addCoupon(coupon);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void updateStatus(Coupon coupon) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CouponDao mapper = sqlSession.getMapper(CouponDao.class);
        mapper.updateStatus(coupon);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<Coupon> hasCouponByOnlyId(Coupon target) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CouponDao mapper = sqlSession.getMapper(CouponDao.class);
        List<Coupon> result = mapper.hasCouponByOnlyId(target);
        sqlSession.close();
        return result;
    }

    @Override
    public Coupon hasAdded(Coupon target) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CouponDao mapper = sqlSession.getMapper(CouponDao.class);
        Coupon result = mapper.hasAdded(target);
        sqlSession.close();
        return result;
    }

    @Override
    public List<Coupon> selectAllCanUseCoupon(Coupon target) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CouponDao mapper = sqlSession.getMapper(CouponDao.class);
        List<Coupon> coupons = mapper.selectAllCanUseCoupon(target);
        sqlSession.close();
        return coupons;
    }

}
